//
// File: polyfit.cpp
//
// MATLAB Coder version            : 5.4
// C/C++ source code generated on  : 23-Apr-2025 16:26:18
//

// Include Files
#include "polyfit.h"
#include "qrsolve.h"
#include "rt_nonfinite.h"
#include "xgeqp3.h"
#include "coder_array.h"
#include <string.h>

// Function Definitions
//
// Arguments    : const ::coder::array<double, 2U> &x
//                const ::coder::array<double, 2U> &y
//                double p[2]
// Return Type  : void
//
namespace coder {
void polyfit(const ::coder::array<double, 2U> &x,
             const ::coder::array<double, 2U> &y, double p[2])
{
  array<double, 2U> V;
  array<double, 1U> b_y;
  double tau_data[2];
  int jpvt[2];
  int assumedRank;
  int i;
  int mn;
  V.set_size(x.size(1), 2);
  if (x.size(1) != 0) {
    i = x.size(1);
    for (int k{0}; k < i; k++) {
      V[k + V.size(0)] = 1.0;
    }
    i = x.size(1);
    for (int k{0}; k < i; k++) {
      V[k] = x[k];
    }
  }
  internal::lapack::xgeqp3(V, tau_data, &mn, jpvt);
  assumedRank = 0;
  mn = V.size(0);
  if (mn > 2) {
    mn = 2;
  }
  if (mn > 0) {
    for (int k{0}; k < mn; k++) {
      if (V[k + V.size(0) * k] != 0.0) {
        assumedRank++;
      }
    }
  }
  mn = y.size(1);
  b_y.set_size(mn);
  mn--;
  for (i = 0; i <= mn; i++) {
    b_y[i] = y[i];
  }
  internal::LSQFromQR(V, tau_data, jpvt, b_y, assumedRank, p);
}

} // namespace coder

//
// File trailer for polyfit.cpp
//
// [EOF]
//
